import 'dart:convert' as convert;
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_web_study/api/dio_utils.dart';
import 'package:flutter_web_study/utils/toast_utils.dart';
import 'package:logger/logger.dart';

import '../api/service_api.dart';
import '../bean/menu_item.dart';
import '../router/application.dart';
import '../router/routes.dart';

class CommodityEvaluationViewModel extends ChangeNotifier {
  int page1 = 1;
  int page2 = 2;

  MenuItemBean menuItemBean =
      MenuItemBean(value: 0, label: "全部", checked: false);

  // //品牌名称
  // final TextEditingController brandNameEidtText = TextEditingController();
  //
  // //系列名称
  // final TextEditingController seriesNameEidtText = TextEditingController();
  //
  // //色号名称
  // final TextEditingController colorNameEidtText = TextEditingController();

  //所有商品
  List listShopAll = [];
  List listShopAllSelect = [];
  BuildContext me;

  //总页数
  var sumNumber = 0;

  //商品分类

  CommodityEvaluationViewModel(this.me);

  initView() {
    // 请求接口
    DioUtils.getInstance()?.post(ServiceApi.QUERY_GOODS_COMMENT, {
      "startTime": "",
      "endTime": "",
      "page": page1,
      "pageSize": 20,
    }, (successCallBack) {
      listShopAll = successCallBack["data"]["list"];
      sumNumber = successCallBack["data"]["totalPage"];
      notifyListeners();
    }, (errorCallBack) {});
  }

  initData() {}

  //多选
  multipleChoice(value) {
    if (listShopAllSelect.contains(value)) {
      listShopAllSelect.remove(value);
    } else {
      listShopAllSelect.add(value);
    }
    notifyListeners();
  }

  // newShopJump() {
  //   Application().navigatePushToPage(me, Routes.newShopPage, {}).then((value) {
  //     if (value != null) {
  //       initView();
  //     }
  //   });
  // }

  //编辑商品
  // editShopJump(value) {
  //   Application().navigatePushToPage(me, Routes.newShopPage,
  //       {"jsonBean": convert.jsonEncode(value)}).then((value1) {
  //     Logger().d(value1);
  //     if (value1 != null) {
  //       for (int i = 0; i < listShopAll.length; i++) {
  //         if (value1["goodsId"] == listShopAll[i]["goodsId"]) {
  //           listShopAll[i] = value1;
  //           notifyListeners();
  //         }
  //       }
  //     }
  //   });
  // }

  //翻页
  addPage(bool value) {
    print("=======$value");
    if (value == true) {
      // +
      if (page1 == sumNumber) {
        ToastUtils.showToast(msg: "已经是最后一页了");
        return;
      }
      page1++;
      page2++;
    } else {
      // -
      if (page1 == 1) {
        ToastUtils.showToast(msg: "已经是第一页了");
        return;
      }
      page1--;
      page2--;
    }
    notifyListeners();
    initView();
  }

//编辑评论
  editComment(value) {
    Application().navigatePushToPage(me, Routes.productReviewPage,
        {"jsonBean": convert.jsonEncode(value), "tagPage": 1}).then((value1) {
      if (value1 != null) {
        value = value1;
        notifyListeners();
      }
    });
  }

  //删除商品
  deleteComment(value) {
    DioUtils.getInstance()?.post(ServiceApi.DELETE_GOODS_COMMENT, {
      "goodsId": value["goodsId"],
      "commentId": value["commentId"]
    }, (successCallBack) {
      ToastUtils.showToast(msg: "${successCallBack["message"]}");
      listShopAll.remove(value);
      listShopAllSelect.remove(value);
      notifyListeners();
    }, (errorCallBack) {
      initView();
      ToastUtils.showToast(msg: "${errorCallBack["message"]}");
    });
  }

  concentration(value) {
    var boo = value["isPrime"] ? 0 : 1;
    DioUtils.getInstance()?.post(ServiceApi.SET_GOODS_COMMENT_PRIME,
        {"commentId": value["commentId"], "type": boo}, (successCallBack) {
      value["isPrime"] = !value["isPrime"];
      notifyListeners();
    }, (errorCallBack) {
      initView();
      ToastUtils.showToast(msg: "${errorCallBack["message"]}");
    });
  }

  // copyShop(value) {
  //   value["goodsId"] = "";
  //   DioUtils.getInstance()
  //       ?.post(ServiceApi.addMakeupGoods, {"json": jsonEncode(value)},
  //           (successCallBack) {
  //     ToastUtils.showToast(msg: "复制成功");
  //     initView();
  //   }, (errorCallBack) {});
  // }

  getMenuItemBean(String value) {
    brandTypeList.forEach((element) {
      if (value == element.label) {
        menuItemBean = element;
      }
    });
    notifyListeners();
  }

  queryDate() {
    initView();
  }
}
